package me.xplabs.controller
{
	import me.xplabs.build.UpgradeBuildingMediator;
	import me.xplabs.constant.GameResultType;
	import me.xplabs.Globals;
	import me.xplabs.model.msg.MsgChangeRaidInfo;
	import me.xplabs.model.proxy.MMapManagerProxy;
	import me.xplabs.model.proxy.RaidInfoProxy;
	import me.xplabs.msg.NotificationFight;
	import me.xplabs.msg.NotificationName;
	import me.xplabs.msg.NotificationRaidInfo;
	import me.xplabs.view.RaidInfoViewMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	/**
	 * http://www.xplabs.me/
	 * @author xiaohan
	 */
	public class RaidInfoCommand extends SimpleCommand
	{
		private var _raidInfoProxy:RaidInfoProxy;
		private var _raidInfoViewMediator:RaidInfoViewMediator;
		private var _upgradeBuildingMediator:UpgradeBuildingMediator;
		
		public function RaidInfoCommand()
		{
		
		}
		
		override public function execute(notification:INotification):void
		{
			if (!_raidInfoProxy)
			{
				_raidInfoProxy = RaidInfoProxy(facade.retrieveProxy(RaidInfoProxy.NAME));
				if (!_raidInfoProxy)
				{
					facade.registerProxy(new RaidInfoProxy());
					_raidInfoProxy = RaidInfoProxy(facade.retrieveProxy(RaidInfoProxy.NAME));
				}
			}
			if (!_raidInfoViewMediator)
				_raidInfoViewMediator = RaidInfoViewMediator(facade.retrieveMediator(RaidInfoViewMediator.NAME));
			if (!_upgradeBuildingMediator)
				_upgradeBuildingMediator = UpgradeBuildingMediator(facade.retrieveMediator(UpgradeBuildingMediator.NAME));
			switch (notification.getType())
			{
				case NotificationRaidInfo.INIT_DATE_RAIDINFO: 
					var msg:MsgChangeRaidInfo = MsgChangeRaidInfo(notification.getBody());
					_raidInfoProxy.curWave = msg.wave;
					_raidInfoProxy.totalWave = msg.totalWave;
					_raidInfoProxy.gold = msg.gold;
					_raidInfoProxy.life = msg.life;
					_raidInfoProxy.gameResult = false;
					if (_raidInfoViewMediator)
						_raidInfoViewMediator.raidInfoView.initRaidInfo(_raidInfoProxy.life, _raidInfoProxy.gold, _raidInfoProxy.curWave, _raidInfoProxy.totalWave);
					break;
				case NotificationRaidInfo.INIT_VIEW_RAIDINFO: 
					if (_raidInfoViewMediator)
						_raidInfoViewMediator.raidInfoView.initRaidInfo(_raidInfoProxy.life, _raidInfoProxy.gold, _raidInfoProxy.curWave, _raidInfoProxy.totalWave);
					break;
				case NotificationRaidInfo.UPDATE_GOLD: 
					_raidInfoProxy.gold += int(notification.getBody());
					if (_raidInfoViewMediator)
						_raidInfoViewMediator.raidInfoView.updateGold(_raidInfoProxy.gold);
					if (_upgradeBuildingMediator)
						_upgradeBuildingMediator.updateGold();
					break;
				case NotificationRaidInfo.UPDATE_LIFE: 
					_raidInfoProxy.life += int(notification.getBody());
					if (_raidInfoProxy.life <= 0 && !_raidInfoProxy.gameResult)
					{
						//Globals.timeManager.addDelay(5, function():void
						//{
						Globals.timeManager.delayFrame(function():void
						{
							sendMsg(NotificationName.END_GAME, GameResultType.FAIL);
							sendMsg(NotificationName.FIGHT_MAP_MANAGER, null, NotificationFight.SUSPEND_GAME);
							MMapManagerProxy(facade.retrieveProxy(MMapManagerProxy.NAME)).dispose();
							_raidInfoProxy.gameResult = true;
						//});
						});
					}
					if (_raidInfoViewMediator)
						_raidInfoViewMediator.raidInfoView.updateLife(_raidInfoProxy.life);
					break;
				case NotificationRaidInfo.UPDATE_CURWAVE: 
					_raidInfoProxy.curWave = int(notification.getBody());
					if (_raidInfoViewMediator)
						_raidInfoViewMediator.raidInfoView.updateWave(_raidInfoProxy.curWave, _raidInfoProxy.totalWave);
					break;
				case NotificationRaidInfo.END_GAME: 
					if (!_raidInfoProxy.gameResult)
					{
						sendMsg(NotificationName.END_GAME, GameResultType.WIN);
						Globals.timeManager.delayFrame(function():void
						{
							sendMsg(NotificationName.FIGHT_MAP_MANAGER, null, NotificationFight.SUSPEND_GAME);
						});
						MMapManagerProxy(facade.retrieveProxy(MMapManagerProxy.NAME)).dispose();
						_raidInfoProxy.gameResult = true;
					}
					break;
			}
		}
	}

}